package com.example.catfish.news.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.catfish.news.R;
import com.example.catfish.news.adapter.TabAdapter;
import com.example.catfish.news.fragment.GovFragment;
import com.example.catfish.news.fragment.HomeFragment;
import com.example.catfish.news.fragment.NewsFragment;
import com.example.catfish.news.fragment.ServiceFragment;
import com.example.catfish.news.fragment.SettingFragment;
import com.example.catfish.news.views.NoScrollViewPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.vp)
    NoScrollViewPager vp;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_newscenter)
    RadioButton rbNewscenter;
    @BindView(R.id.rb_service)
    RadioButton rbService;
    @BindView(R.id.rb_govaffairs)
    RadioButton rbGovaffairs;
    @BindView(R.id.rb_setting)
    RadioButton rbSetting;
    @BindView(R.id.rg_tab)
    RadioGroup rgTab;

    private List<Fragment> fragments;

    /*  private NoScrollViewPager vp;
        private RadioGroup rg_tab;
        private RadioButton rb_home;
        private RadioButton rb_news;
        private RadioButton rb_service;
        private RadioButton rb_gov;
        private RadioButton rb_setting;
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initViewPager();
        initSlidingMenu();
    }

    /**
     * 初始化侧边栏
     */
    private void initSlidingMenu() {
        setTitle("我是标题");
        // configure the SlidingMenu
        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        //设置全屏可用
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //设置侧滑菜单的宽度
        menu.setBehindWidth(710);
       /* menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);*/
        //把侧划菜单附加到Activity中
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu);
    }


    private void initView() {
        //底部按钮的与ViewPager滑动的绑定
        rgTab.setOnCheckedChangeListener(this);
        //ViewPager与底部按钮的滑动绑定
        vp.addOnPageChangeListener(this);
    }

    /**
     * 初始化viewpager
     */
    private void initViewPager() {
        GovFragment govFragment = new GovFragment();
        HomeFragment homeFragment = new HomeFragment();
        NewsFragment newsFragment = new NewsFragment();
        ServiceFragment serviceFragment = new ServiceFragment();
        SettingFragment settingFragment = new SettingFragment();
        fragments = new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(newsFragment);
        fragments.add(serviceFragment);
        fragments.add(govFragment);
        fragments.add(settingFragment);
        TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager(), fragments);
        vp.setAdapter(tabAdapter);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        int position = 0;
        switch (i) {
            case R.id.rb_home:
                position = 0;
                break;
            case R.id.rb_newscenter:
                position = 1;
                break;
            case R.id.rb_service:
                position = 2;
                break;
            case R.id.rb_govaffairs:
                position = 3;
                break;
            case R.id.rb_setting:
                position = 4;
                break;
        }
        vp.setCurrentItem(position, false);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                rbHome.setChecked(true);
                break;
            case 1:
                rbNewscenter.setChecked(true);
                break;
            case 2:
                rbService.setChecked(true);
                break;
            case 3:
                rbGovaffairs.setChecked(true);
                break;
            case 4:
                rbSetting.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
